﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.BizCode;
using System.Data;
using Wicresoft.Common;

namespace WsValid
{
    public class ListAttribute : Attribute
    {
    }

    public class SortAttribute : Attribute
    {
    }

    public class SearchAttribute : Attribute
    {
    }

    public class DefaultSortAttribute : Attribute
    {
    }


    public class WSAuthAttribute : AuthorizeAttribute
    {
        public bool IsAndAuthorize = false;

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (!this.AuthorizeCore(filterContext.HttpContext))
            {
                this.HandleUnauthorizedRequest(filterContext);
            }
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            string[] _usersSplit;
            string[] _rolesSplit;
            _usersSplit = Users.Split(';', ',');
            _rolesSplit = Roles.Split(';', ',');
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            Wicresoft.Framework.Organization.User user = Consts.CurrentUser;
            if (IsAndAuthorize)
            {
                if (!string.IsNullOrEmpty(Users) && !_usersSplit.Contains<string>(user.Name, StringComparer.OrdinalIgnoreCase))
                {
                    return false;
                }
                if (!string.IsNullOrEmpty(Roles) && !_rolesSplit.Any<string>(new Func<string, bool>(IsInRole)))
                {
                    return false;
                }
                return true;
            }
            else
            {
                if (!string.IsNullOrEmpty(Users) && _usersSplit.Contains<string>(user.Name, StringComparer.OrdinalIgnoreCase))
                {
                    return true;
                }
                if (!string.IsNullOrEmpty(Roles) && _rolesSplit.Any<string>(new Func<string, bool>(IsInRole)))
                {
                    return true;
                }
                return false;
            }

        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }

        protected bool IsInRole(string role)
        {

            DataTable dt = DataAccess.ExecuteSql("Wicresoft.Framework.ConnectionString", "select count(1) from relation where parentnodeid='" + role + "' and Childnodeid='" + Consts.CurrentUserID + "'").Tables[0];
            return int.Parse(dt.Rows[0][0].ToString()) == 1;
            //return (Wicresoft.Framework.Organization.OrganizationHelper.IsUserInRole(Consts.CurrentUser.ID, new Guid(role)));
        }
    }
}
